﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WEB.Model.Entity.Accout;
using WEB.Utils;
using System.Data.SqlClient;
using System.Data;

namespace WEB.Model.Service
{
    public class AdminService
    {
        DataAccess da = new DataAccess();
        /// <summary>
        /// 按ID 值获得用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Admin getAdminById(int id)
        {
            Admin admin = new Admin();
            string sql = "select * from t_admin where aid = " + id + " order by aid asc";

            SqlDataReader reader = da.getReader(sql);

            if (reader.HasRows)
            {
                reader.Read();
                try
                {
                    admin.aid = Convert.ToInt32(reader["aid"]);
                    admin.eid = Convert.ToInt32(reader["eid"]);
                    admin.username = Convert.ToString(reader["a_username"]);
                    admin.passwd = Convert.ToString(reader["a_passwd"]);
                    admin.email = Convert.ToString(reader["a_email"]);
                    admin.job = Convert.ToString(reader["a_job"]);
                    admin.birthday = Convert.ToDateTime(reader["a_birthday"]);
                    admin.tel = Convert.ToString(reader["a_tel"]);
                    admin.role = Convert.ToInt32(reader["a_role"]);
                    admin.aStatus = Convert.ToInt32(reader["a_status"]);
                    admin.aType = Convert.ToInt32(reader["a_type"]);
                    admin.qq = Convert.ToString(reader["a_qq"]);
                    admin.msn = Convert.ToString(reader["a_msn"]);
                    admin.address = Convert.ToString(reader["a_address"]);
                    admin.phone = Convert.ToString(reader["a_phone"]);
                    admin.sex = Convert.ToString(reader["a_sex"]);
                    admin.name = Convert.ToString(reader["a_name"]);
                }
                catch (Exception ee)
                {
                    Console.WriteLine(ee.Message);
                }
            }
            reader.Close();
            return admin;
        }
        /// <summary>
        /// 按 E-mail 检索管理员
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public Admin getAdminByEmail(string email)
        {
            Admin admin = new Admin();
            string sql = "select * from t_admin where a_email = '" + email + "' order by aid asc";

            SqlDataReader reader = da.getReader(sql);

            if (reader.HasRows)
            {
                reader.Read();
                admin.aid = Convert.ToInt32(reader["aid"]);
                admin.eid = Convert.ToInt32(reader["eid"]);
                admin.username = Convert.ToString(reader["a_username"]);
                admin.passwd = Convert.ToString(reader["a_passwd"]);
                admin.email = Convert.ToString(reader["a_email"]);
                admin.job = Convert.ToString(reader["a_job"]);
                admin.birthday = Convert.ToDateTime(reader["a_birthday"]);
                admin.tel = Convert.ToString(reader["a_tel"]);
                admin.role = Convert.ToInt32(reader["a_role"]);
                admin.aStatus = Convert.ToInt32(reader["a_status"]);
                admin.aType = Convert.ToInt32(reader["a_type"]);
                admin.qq = Convert.ToString(reader["a_qq"]);
                admin.msn = Convert.ToString(reader["a_msn"]);
                admin.address = Convert.ToString(reader["a_address"]);
                admin.phone = Convert.ToString(reader["a_phone"]);
                admin.sex = Convert.ToString(reader["a_sex"]);
                admin.name = Convert.ToString(reader["a_name"]);
            }
            else
            {
                return null;
            }
            reader.Close();
            return admin;
        }
        /// <summary>
        /// 按用户名检索用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>用户实体</returns>
        public Admin getAdminByUsername(string username)
        {
            Admin admin = new Admin();
            string sql = "select * from t_admin where a_username = '" + username + "' order by aid asc";

            SqlDataReader reader = da.getReader(sql);

            if (reader.HasRows)
            {
                reader.Read();
                if (reader["aid"].ToString() == "")
                {
                    admin.aid = -1;
                }
                else
                {
                    admin.aid = Convert.ToInt32(reader["aid"]);
                }
                admin.eid = Convert.ToInt32(reader["eid"]);
                admin.username = Convert.ToString(reader["a_username"]);
                admin.passwd = Convert.ToString(reader["a_passwd"]);
                //admin.email = Convert.ToString(reader["a_email"]);
                //admin.job = Convert.ToString(reader["a_job"]);
                if ( reader["a_birthday"] == null)
                {
                    admin.birthday = default(DateTime);
                }
                else
                {
                    admin.birthday = Convert.ToDateTime(reader["a_birthday"]);
                }
                //admin.birthday = Convert.ToDateTime(reader["a_birthday"]);
                if (reader["a_tel"].ToString() == "")
                {
                    admin.tel = default(String);
                }
                {
                    admin.tel = reader["a_tel"].ToString();
                }
                //admin.tel = Convert.ToString(reader["a_tel"]);
                //admin.role = Convert.ToInt32(reader["a_role"]);
                //admin.aStatus = Convert.ToInt32(reader["a_status"]);
                //admin.aType = Convert.ToInt32(reader["a_type"]);
                //admin.qq = Convert.ToString(reader["a_qq"]);
                //admin.msn = Convert.ToString(reader["a_msn"]);
                //admin.address = Convert.ToString(reader["a_address"]);
                //admin.phone = Convert.ToString(reader["a_phone"]);
                //admin.sex = Convert.ToString(reader["a_sex"]);
                //admin.name = Convert.ToString(reader["a_name"]);

            }
            else
            {
                return null;
            }
            reader.Close();
            return admin;
        }

        public List<Admin> getAdmins()
        {
            List<Admin> admins = new List<Admin>();
            Admin admin = null;
            string sql = "select * from t_admin order by aid asc";
            SqlDataReader reader = da.getReader(sql);

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    admin = new Admin();
                    admin.aid = Convert.ToInt32(reader["aid"]);
                    admin.eid = Convert.ToInt32(reader["eid"]);
                    admin.username = Convert.ToString(reader["a_username"]);
                    admin.passwd = Convert.ToString(reader["a_passwd"]);
                    admin.email = Convert.ToString(reader["a_email"]);
                    admin.job = Convert.ToString(reader["a_job"]);
                    admin.birthday = Convert.ToDateTime(reader["a_birthday"]);
                    admin.tel = Convert.ToString(reader["a_tel"]);
                    admin.role = Convert.ToInt32(reader["a_role"]);
                    admin.aStatus = Convert.ToInt32(reader["a_status"]);
                    admin.aType = Convert.ToInt32(reader["a_type"]);
                    admin.qq = Convert.ToString(reader["a_qq"]);
                    admin.msn = Convert.ToString(reader["a_msn"]);
                    admin.address = Convert.ToString(reader["a_address"]);
                    admin.phone = Convert.ToString(reader["a_phone"]);
                    admin.sex = Convert.ToString(reader["a_sex"]);
                    admin.name = Convert.ToString(reader["a_name"]);
                    admins.Add(admin);
                }
            }
            else
            {
                reader.Close();
                return null;
            }
            reader.Close();
            return admins;
        }

    }
}